import { fileURLToPath, URL } from 'node:url';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueDevTools from 'vite-plugin-vue-devtools';
// import path from 'path';
import AutoImport from 'unplugin-auto-import/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import IconsResolver from 'unplugin-icons/resolver';
import Components from 'unplugin-vue-components/vite';
import Inspect from 'vite-plugin-inspect';
// import Icons from 'unplugin-icons/vite';
// import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import VueSetupExtend from 'vite-plugin-vue-setup-extend';
const pathSrc = fileURLToPath(new URL('./src', import.meta.url));

export default defineConfig(({ command }) => {
  const isDev = command === 'serve';
  return {
    plugins: [
      vue(),
      vueDevTools(),
      VueSetupExtend(),
      // https://github.com/antfu/unplugin-auto-import
      AutoImport({
        imports: [
          'vue',
          'pinia',
          'vue-router',
          {
            from: 'vue-router',
            imports: ['RouteLocationRaw', 'RouteLocationNormalizedLoaded'],
            type: true,
          },
        ],

        // Options for scanning directories for auto import
        dirsScanOptions: {
          filePatterns: ['*.ts'], // Glob patterns for matching files
          fileFilter: file => file.endsWith('.ts'), // Filter files
          types: true, // Enable auto import the types under the directories
        },
        dirs: [
          'src/api/**',
          'src/components/**',
          'src/stores/modules/**',
          'src/type/**',
          'src/util/**',
        ],
        dtsMode: 'overwrite',
        dts: 'src/auto-imports.d.ts',
        resolvers: [
          ElementPlusResolver(),
          IconsResolver({
            prefix: 'Icon',
          }),
        ],
      }),
      // https://github.com/antfu/unplugin-vue-components
      Components({
        // allow auto load markdown components under `./src/components/`
        extensions: ['vue', 'md'],
        // allow auto import and register components used in markdown
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
        dts: 'src/components.d.ts',
        resolvers: [
          ElementPlusResolver(),
          IconsResolver({
            enabledCollections: ['ep'],
          }),
        ],
      }),
      Inspect(),
    ],

    resolve: {
      alias: {
        '@': pathSrc,
      },
    },

    preview: {
      host: true,
      port: 8888,
    },

    server: {
      host: true,
      port: 8888,
      strictPort: true,
      cors: true,
      proxy: {
        '/api': {
          target: isDev ? 'http://127.0.0.1:8000' : 'http://101.37.105.53:8000',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, ''),
        },
      },
      hmr: {
        overlay: false,
      },
    },
    build: {
      rollupOptions: {
        output: {
          // 可以在这里配置资源的输出路径和文件名格式
          assetFileNames: 'assets/[name].[ext]',
        },
      },
    },
  };
});
